/*
 * Project: Payment
 * Document: GeneralMerchant
 * Date: 2020/9/19 11:19 上午
 * Author: wangbz
 *
 * Copyright © 2020 www.ixiachong.com Inc. All rights reserved.
 * 注意：本内容仅限于深圳瞎充集团有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
package com.factory;

import java.util.Map;

public abstract class GeneralMerchant<C> {
    protected String no;
    protected Map<String, String> configs;
    private C client;
    private boolean isSingleClient;

    public GeneralMerchant(String no, Map<String, String> configs, boolean isSingleClient) {
        this.no = no;
        this.configs = configs;
        this.isSingleClient = isSingleClient;
    }

    public C getClient() {
        if (isSingleClient) {
            if (client == null) {
                synchronized (this) {
                    if (client == null) {
                        client = newClient();
                    }
                }
            }
            return client;
        } else {
            return newClient();
        }
    }

    protected abstract C newClient();
}
